var bind_js = {};
bind_js.bind = function(){
	//每次动画完结过要除去动画样式
	$(document).on('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend','.animated',function(){
		$(this).removeClass('shake');
	});		
}
bind_js.init = function(){
	bind_js.bind();
}
$(document).ready(function(){
	bind_js.init();
});
var vm = avalon.define({
	$id:'main_controller',
	obj:{
		//手机号码
		mobile:'',
		//姓名
		realname:'',
		//短信验证码
		code:'',
		//回调值
		callback:callback_val,
	},
	//按钮文本变化
	btn_text:"获取短信验证码",
	//60秒
	$count:60,
	//是否正在处于60秒禁止状态中
	is_loading:false,
	//追加变量防止重复提交
	$is_do:false,	
	//提交
	/**
	 * 流程
	 * 1、验证输入的短信验证码
	 * 2、绑定流程
	 */
	submit:function(){
		if(vm.$is_do){
			return ;
		}		
		//这里要验证手机号码
		if(volidate_js.is_phone(vm.obj.mobile)==false){
			if(vm.obj.mobile.trim()){
				vm.toast_msg = "手机号码格式有误";
			}else{
				vm.toast_msg = "请输入手机号码";
			}
			vm.is_toast_show = true;
			vm.clear_toast();
			return ;
		}	
		if(!vm.obj.realname.trim()){
			vm.toast_msg = "请输入您的姓名";
			vm.is_toast_show = true;
			vm.clear_toast();			
			return ;
		}		
		if(!vm.obj.code.trim()){
			vm.toast_msg = "输入短信验证码";
			vm.is_toast_show = true;
			vm.clear_toast();			
			return ;
		}
		var $this = $(this);
		$this.button('loading');
		var success_fun = function(){
			$.ajax({
				type:"post",
				dataType:"json",
				//追加手机号进行双重判断
				data:'verify='+vm.obj.code+"&phone="+vm.obj.mobile,
				url:get_phone_verify_url,
				error:function(){
					vm.is_loading = false;
					$this.button('reset');				
					vm.toast_msg = "网络错误";
					vm.is_toast_show = true;
					vm.clear_toast();
				},
				success:function(data){
					$this.button('reset');
					vm.is_loading = false;
					if(data.status ==1){
						vm.$is_do = true;
						//成功之后直接提交表单
						document.getElementById("form").submit();
					}else if(data.status == 2){
						vm.toast_msg = "验证码错误";
						vm.is_toast_show = true;
						vm.clear_toast();					
					}else if(data.status == 3){
						vm.toast_msg = "手机号不匹配";
						vm.is_toast_show = true;
						vm.clear_toast();					
					}else{
						vm.toast_msg = "服务器异常";
						vm.is_toast_show = true;
						vm.clear_toast();					
					}
				},
			});			
		}
		success_fun();
	},
	//获取短信验证码
	/**
	 * 流程：
	 * 1、验证手机号是否可用
	 * 2、发送短信验证码
	 */
	get_code:function(){
		if(vm.is_loading){
			return ;
		}
		//这里要验证手机号码
		if(volidate_js.is_phone(vm.obj.mobile)==false){
			if(vm.obj.mobile.trim()){
				vm.toast_msg = "手机号码格式有误";
			}else{
				vm.toast_msg = "请输入手机号码";
			}
			vm.is_toast_show = true;
			vm.clear_toast();
			return ;
		}
		var $this = $(this);
		$this.button('loading');
		vm.is_loading = true;
		var success_fun = function(){
			$.ajax({
				type:"post",
				url:sand_sms_url,
				dataType:"json",
				data:"phone="+vm.obj.mobile,
				success:function(data){
					if(data.status == 1){
						$this.html(vm.$count).addClass('disabled');
						if(vm.$count_timer){
							clearInterval(vm.$count_timer);
						}
						vm.$count_timer = setInterval(function() {
							if (vm.$count) {
								vm.$count--;
								$this.html(vm.$count);
							} else {
								//到了0秒
								vm.is_loading = false;
								clearInterval(vm.$count_timer);
								$this.button('reset');
								vm.$count = 60;
							}
						}, 1000);					
					}else if(data.status ==2 ){
						vm.is_loading = false;
						$this.button('reset');
						vm.toast_msg = "发送失败";
						vm.is_toast_show = true;
						vm.clear_toast();					
					}else{
						vm.is_loading = false;
						$this.button('reset');					
						vm.toast_msg = "服务器异常";
						vm.is_toast_show = true;
						vm.clear_toast();					
					}
					
				},
				error:function(){
					vm.is_loading = false;
					$this.button('reset');				
					vm.toast_msg = "网络错误";
					vm.is_toast_show = true;
					vm.clear_toast();
				},
			});			
		}
		//验证手机号是否可用
		$.ajax({
			type:"post",
			url:check_mobile_url,
			dataType:"json",
			data:'phone='+vm.obj.mobile,			
			error:function(){
				vm.is_loading = false;
				$this.button('reset');				
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();				
			},
			success:function(data){
				if (data.status == 1) {
					//确定手机号可用后发送短信验证码
					success_fun();
				} else if (data.status == 2) {
					vm.is_loading = false;
					$this.button('reset');
					vm.toast_msg = "手机号已被占用";
					vm.is_toast_show = true;
					vm.clear_toast();
				} else {
					vm.is_loading = false;
					$this.button('reset');
					vm.toast_msg = "服务器异常";
					vm.is_toast_show = true;
					vm.clear_toast();
				}			
			},
		});		

	},
	//记录时间任务的id
	$count_timer:0,
	
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------
	
});
